// pages/me/me.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cons:[
      '我的订单',
      '旅盒大揭秘',
      '旅盒知多点',
      '乳品科普站',
      '常见问题',
      '了解我们'
    ],
    imgs: [
      "http://delice.riki.cn/mavista/img/collection-icon.png",
      'http://delice.riki.cn/mavista/img/pioneer.png',
      'http://delice.riki.cn/mavista/img/explorer.png',
      'http://delice.riki.cn/mavista/img/adventurer.png',
      'http://delice.riki.cn/mavista/img/expedition Leader.png'
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    clearInterval(this.countTime)
    wx.getUserInfo({
      success(res) {
        _this.setData({
          userInfo: res.userInfo
        })
      }
    })
    wx.getSystemInfo({
      success: function(res) {
        console.log(res)
        _this.setData({
          screenHeight: res.screenHeight
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let authCode = { authCode: wx.getStorageSync('authCode')};
    let _this = this;
    app.requestData('/api/purchase/order/unpaid',authCode,'GET',function(res){
      console.log(res)
      let timestamp = res.created;
      let time = new Date(timestamp);
      let year = time.getFullYear();
      let month = change(time.getMonth() + 1);
      let day = change(time.getDate());
      let hour = change(time.getHours());
      let minute = change(time.getMinutes());
      // let second = change(time.getSeconds());
      let orderTime = year + '年' + month + '月' + day + '日 ' +' '+ hour + ':' + minute;  
      function change(t) {
        if (t < 10) {
          return "0" + t;
        } else {
          return t;
        }
      }  
      clearInterval(_this.countTime)
      let authCode = {
        authCode: wx.getStorageSync("authCode")
      }
      app.requestData('/api/purchase/order/contain_unpaid', authCode, 'get', function (res) {
        console.log(res)
        if (res.hasUnpaidOrder) {
          let count = 10
          _this.countTime= setInterval(function () {
            if (count > 0) {
              count--;
              _this.setData({
                count: count,
                show3: true,
                orderShow:true
              })
              console.log(count)

            } else {
              clearInterval(_this.countTime)
              wx.navigateTo({
                url: '../search/product/orderDetail/orderDetail',
              })
            }
          }, 1000)

        }else {
          _this.setData({
            orderShow:false
          })
        }
      }) 

      // console.log(time)
      _this.setData({
        order:res,
        orderTime: orderTime
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // console.log('onunload')
    clearInterval(this.countTime)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
   
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  orderDetail() {
    clearInterval(this.countTime)
    wx.navigateTo({
      url: '../search/product/orderDetail/orderDetail',
    })
  },

})